﻿using ERPPublic;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Components.Forms;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using WarehouseManagement.Application.Command.OutboundShipmentsCommand;
using WarehouseManagement.Application.Command.RequestTransferCommand;
using WarehouseManagement.Application.Command.WarehouseCommand;
using WarehouseManagement.Domain.DTO.OutvoundRequests;
using WarehouseManagement.Domain.DTO.RequestTransfer;
using WarehouseManagement.Domain.DTO.ShipApply;
using WarehouseManagement.Domain.Entity;

namespace WarehouseManagement.Controllers
{
    /// <summary>
    /// 仓库调拨申请控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [Authorize]
    [ApiController]
    public class RequestTransfersController : ControllerBase
    {
        protected readonly IMediator mediatorp;
        public RequestTransfersController(IMediator mediatorp)
        {
            this.mediatorp = mediatorp;
        }
        /// <summary>
        /// 仓库调拨申请添加
        /// </summary>
        /// <param name="cd">要添加的实体</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> RequestTransfersAdd(RequestTransferCreateCommand cd)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                cd.addDto.Office_Id = sid;
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                cd.addDto.AddName = name;
                //获取用户ID
                var userId = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
                cd.addDto.User_Id = int.Parse(userId);
                result.Data = await mediatorp.Send(cd);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 仓库调拨申请列表信息展示
        /// </summary>
        /// <param name="cd">查询条件</param>
        /// <returns>返回集合</returns>
        [HttpGet]
        public async Task<ApiResult<PageDTO<SelectRequestTransfersDto>>> ShowRequestTransfers([FromQuery] RequestTransferSelectCommand cd)
        {
            ApiResult<PageDTO<SelectRequestTransfersDto>> result = new ApiResult<PageDTO<SelectRequestTransfersDto>>();
            try
            {
                result.Data = await mediatorp.Send(cd);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 调拨申请删除
        /// </summary>
        /// <param name="cd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> RequestTransfersDel(RequestTransferDelCommand cd)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                cd.DelName = name;
                result.Data = await mediatorp.Send(cd);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 调拨申请反填
        /// </summary>
        /// <param name="cd"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<FanRequestTransferDto>> ProcureEnquirySetInfo([FromQuery] RequestTransferFanCommand cd)
        {
            ApiResult<FanRequestTransferDto> result = new ApiResult<FanRequestTransferDto>();
            try
            {
                result.Data = await mediatorp.Send(cd);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 调拨申请修改
        /// </summary>
        /// <param name="cd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> ProcureEnquiryUpdate(RequestTransferUpdCommand cd)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                cd.updDto.Office_Id = sid;
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                cd.updDto.UpdName = name;
                //获取用户ID
                var userId = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
                cd.updDto.User_Id = int.Parse(userId);
                result.Data = await mediatorp.Send(cd);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }
    }
}
